package com.sakura.mall.member.memberReceiveAddress.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.member.memberReceiveAddress.domain.entity.MemberReceiveAddressEntity;
import com.sakura.mall.member.memberReceiveAddress.domain.request.AddMemberReceiveAddressRequest;
import com.sakura.mall.member.memberReceiveAddress.domain.request.UpdateMemberReceiveAddressRequest;
import com.sakura.mall.member.memberReceiveAddress.domain.request.MemberReceiveAddressPageRequest;
import com.sakura.mall.member.memberReceiveAddress.domain.vo.MemberReceiveAddressVo;
import com.sakura.mall.member.memberReceiveAddress.mapper.MemberReceiveAddressMapper;
import com.sakura.mall.member.memberReceiveAddress.service.MemberReceiveAddressService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员收货地址 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class MemberReceiveAddressServiceImpl extends ServiceImpl<MemberReceiveAddressMapper, MemberReceiveAddressEntity> implements MemberReceiveAddressService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddMemberReceiveAddressRequest request) {
        // todo 其他校验逻辑
        MemberReceiveAddressEntity memberReceiveAddress = request.convert(MemberReceiveAddressEntity.class);
        this.save(memberReceiveAddress);
    }

    @Override
    public Page<MemberReceiveAddressVo> queryPage(MemberReceiveAddressPageRequest request) {
        IPage<MemberReceiveAddressEntity> qp = request.toPage();
        LambdaQueryWrapper<MemberReceiveAddressEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<MemberReceiveAddressVo> ret = qp.convert(entity -> entity.convert(MemberReceiveAddressVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateMemberReceiveAddressRequest request) {
        MemberReceiveAddressEntity entity = request.convert(MemberReceiveAddressEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

